package com.micai.springboot.controller;

import com.micai.springboot.base.BaseController;
import com.micai.springboot.dto.UserDto;
import com.micai.springboot.service.UserService;
import com.micai.springboot.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：用户Controller
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:04
 */
@RestController
@RequestMapping(value = "/users")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value="删除用户", notes="删除用户")
    @DeleteMapping(value="/delete/{id}")
    public String delete(@PathVariable Long id) {
        UserDto userDto = userService.getUserById(id);
        if (userDto != null) {
            userService.delete(userDto);
        }
        return "success";
    }

    @ApiOperation(value="用户列表", notes="用户列表")
    @RequestMapping(value = "/list")
    public List<UserVo> list() {
        /*List<UserDto> userDtos = userService.listUsers();
        if (userDtos == null) {
            return null;
        }*/
        List<UserVo> userVos = new ArrayList<UserVo>();
        /*for (UserDto userDto : userDtos) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userDto, userVo);
            userVos.add(userVo);
        }*/
        return userVos;
    }

    @ApiOperation(value="保存用户", notes="保存用户")
    @RequestMapping(value="/save", method= RequestMethod.POST)
    public String save(@RequestBody UserVo userVo) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userVo, userDto);
        userService.save(userDto);
        return "success";
    }

}
